<?php
GLOBAL $m;
$AppUI->savePlace();

if (! $canAccess) {
	$AppUI->setMsg('Access denied', UI_MSG_ERROR);
	$AppUI->redirect();
}
if (isset($_POST['resetAction'])) {
	$AppUI->unsetPOST('ITEM_FILTER');
}
else {
	if (count($_POST) > 0) {
		$AppUI->savePOST('ITEM_FILTER');
	}
	else {
		$_POST = $AppUI->getPOST('ITEM_FILTER');
	}
}

$type =  dPgetParam($_POST, 'type', 0);

require_once(DP_BASE_DIR."/modules/masterdata/mdtype.class.php");
$types= arrayMerge(array(''=>$AppUI->_('--Select Type1--')), getTypeList('ItemType1', true));

$vatCodeArr = dPgetSysVal("ItemVATCode");

// setup the title block
$titleBlock = new CTitleBlock( 'Items' );
$titleBlock->show();

?>
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<tr>
	<td class="<?=$tab == 0 ? 'tabon' : 'taboff'?>" style="padding-left: 10px; padding-right: 10px"
		onclick="go('?m=items')">Items</td>
	<td class="tabox" width="100%">&nbsp;</td>
</tr>
</table>
<table width="100%" border="0" cellpadding="0" cellspacing="2" class="infopanel">
<tr>
<td width="4px">&nbsp;</td>
<td>
<? if ($canEdit) { ?>
<input type="button" class="button" value="<?=$AppUI->_('New items')?>" onclick="doEditItem(0)"/>
<? } ?>
</td>
<td align="right" width="100%">
	<form name="MdItemFilter" action="?m=<?=$m?>" method="post">
		<?= arraySelect($types,'type','style="font-size:10px" onchange="this.form.submit()"',$type)?>
	</form>
</td>
<td width="4px">&nbsp;</td>
</tr>
</table>
<table id="idTableMasterdata" class="list" width="100%" cellspacing="0" cellpadding="2" border="0">
<tbody>
<tr>
	<th nowrap="nowrap">&nbsp;</th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Type' );?></th>
	<!--th nowrap="nowrap"><?php echo $AppUI->_( 'Code' );?></th-->
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Name' );?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Item group' );?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'To be checked' );?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'VAT Code' );?></th>
	<th nowrap="nowrap"><?php echo $AppUI->_( 'Status' );?></th>
	<th nowrap="nowrap" align="center"><?php echo $AppUI->_( 'Delete' );?></th>
</tr>

<?php
// retrieving some dynamic content using an easy database query
// add/show now gradually the einstein quotes
$itemO = new CItem();
$items = $itemO->getItemsList();

$q = new DBQuery;
$q->addTable('items');
$q->addQuery('items.*, t.name as item_type_name, s.status_name as item_status_name');
$q->addJoin('md_type', 't', 't.type_id = item_type1');
$q->addJoin('md_status', 's', 's.status_id = item_status');
$q->addOrder('item_name');
if($type!= null && $type!=''){
	$q->addWhere('item_type1 = '.$type);
}
$items = $q->loadList();

$itemGroupArr = getTypeList("ItemGroup");

if (empty($items)) {?>
<tr class="second">
<td colspan="100"><?php echo $AppUI->_( 'No item available' );?></td>
</tr>
<?php
}
$i = 0;
foreach ($items as $row) {		//parse the array of einstein quotes
	$i++;
?>
<tr class="<?=($i % 2 == 0) ? "second" : "first" ?>">
	<td nowrap="nowrap" width="20">


	<?php if ($canEdit) {	// in case of writePermission on the module show an icon providing edit functionality for the given quote item

		echo "\n".'<a onclick="doEditItem('.$row["item_id"]. ')">';
		echo dPshowImage( 'images/edit.gif' );
		echo "\n</a>";
	}
	?>
	</td>
	<td><?php echo $row["item_type_name"];?></td>
	<!--td> <?php echo $row["item_code"];?></td-->
	<td><?php echo $row["item_name"];?></td>
	<td><?= $itemGroupArr[$row["item_type2"]]?></td>
	<td align="center" width="20px"><?=$row["item_be_check"] == 1 ? dPshowImage( 'images/problem.gif') : "";?></td>
	<td align="right" width="30px"><?=$row["item_vat_code"] < 0 ? "" : $vatCodeArr[$row["item_vat_code"]];?></td>
	<td><?php echo $row["item_status_name"];?></td>
	<td align="center">
	<?php if ($canEdit) {
		showIconLink('images/delete.gif', 'js:doDelete('.$row["item_id"]. ', \''.$row["item_name"].'\')', 'Delete');
	}
	?>
	</td>
</tr>
<?php
}
?>
</tbody>
</table>
<div id="idItemDialog" class="popup" style="visibility: hidden; left: 436px; top: 94.5px; display: none;">
</div>
<script type="text/javascript" language="javascript">
	Table("idTableMasterdata").setNormalHover(1);
</script>
<form name="MdFormDelete" id="MdFormDelete" method="post">
	<input type="hidden" name="dosql" value ="do_delete" />
	<input type="hidden" name="m" value ="items" />
	<input type="hidden" id="del_cc_id" name="item_id" value ="" />
	<input type="hidden" name="del" value ="" />
</form>